package com.xsdl.mediator;

import javax.swing.*;
import java.awt.event.ItemListener;

public class CommendationMediator {

    /* ========== 表彰相关 ========== */
    private final JRadioButton rbYes;
    private final JRadioButton rbNo;
    private final JLabel lblLevel;
    private final JTextField txtLevel;
    private final JLabel lblUnit;
    private final JTextField txtUnit;

    /* ========== 职称证书相关 ========== */
    private final JRadioButton rbTitleYes;
    private final JRadioButton rbTitleNo;
    private final JLabel lblTitleLevel;
    private final JTextField txtTitleLevel;
    private final JLabel lblCertNo;
    private final JTextField txtCertNo;

    public CommendationMediator(JRadioButton rbYes,
                                JRadioButton rbNo,
                                JLabel lblLevel,
                                JTextField txtLevel,
                                JLabel lblUnit,
                                JTextField txtUnit,
                                JRadioButton rbTitleYes,
                                JRadioButton rbTitleNo,
                                JLabel lblTitleLevel,
                                JTextField txtTitleLevel,
                                JLabel lblCertNo,
                                JTextField txtCertNo) {
        this.rbYes = rbYes;
        this.rbNo = rbNo;
        this.lblLevel = lblLevel;
        this.txtLevel = txtLevel;
        this.lblUnit = lblUnit;
        this.txtUnit = txtUnit;

        this.rbTitleYes = rbTitleYes;
        this.rbTitleNo = rbTitleNo;
        this.lblTitleLevel = lblTitleLevel;
        this.txtTitleLevel = txtTitleLevel;
        this.lblCertNo = lblCertNo;
        this.txtCertNo = txtCertNo;

        /* ---- 初始状态 ---- */
        rbNo.setSelected(true);
        rbTitleNo.setSelected(true);
        toggleCommendationFields(false);
        toggleTitleFields(false);

        /* ---- 统一事件处理 ---- */
        ItemListener commendationListener = e -> {
            boolean yes = rbYes.isSelected();
            toggleCommendationFields(yes);
            if (!yes) {
                txtLevel.setText("");
                txtUnit.setText("");
            }
        };
        rbYes.addItemListener(commendationListener);
        rbNo.addItemListener(commendationListener);

        ItemListener titleListener = e -> {
            boolean yes = rbTitleYes.isSelected();
            toggleTitleFields(yes);
            if (!yes) {
                txtTitleLevel.setText("");
                txtCertNo.setText("");
            }
        };
        rbTitleYes.addItemListener(titleListener);
        rbTitleNo.addItemListener(titleListener);
    }

    private void toggleCommendationFields(boolean visible) {
        lblLevel.setVisible(visible);
        txtLevel.setVisible(visible);
        lblUnit.setVisible(visible);
        txtUnit.setVisible(visible);
    }

    private void toggleTitleFields(boolean visible) {
        lblTitleLevel.setVisible(visible);
        txtTitleLevel.setVisible(visible);
        lblCertNo.setVisible(visible);
        txtCertNo.setVisible(visible);
    }
}